<?

class TourList extends db {

	function get_total() {
		$q = 'select count(ID) as total from Tours';
		$r = mysql_query($q);
		if ($r) {
			$row = mysql_fetch_assoc($r);
			return $row['total'];
		} else {
			return Error::MySQL();
		}
	}
	
	function get_tours() {
		$args_list = func_get_args();
		$number_to_get = (int) $args_list[0];
		$start_number = (int) $args_list[1];
		$is_active = $args_list[2];
		
		if (isset($is_active)) {
			$query = "select ID from Tours where is_active = {$is_active} order by start_date desc limit $start_number, $number_to_get";
		} else if ($number_to_get && $start_number) {
			$query = "select ID from Tours order by start_date desc limit $start_number, $number_to_get";
		} else if ($number_to_get) {
			$query = "select ID from Tours order by start_date desc limit 0, $number_to_get";
		} else if ($start_number) {
			$query = "select ID from Tours order by start_date desc limit $start_number, 10";
		} else {
			$query = "select ID from Tours order by start_date desc limit 0, 10";
		}
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}
		$tour = array();
		while ($row = mysql_fetch_assoc($r)) {
			$tour[] = Tour::get($row['ID']);
		}
		return $tour;
	}
	
}

class Tour extends db {

	var $ID, $start_date, $end_date, $title, $description, $is_active, $av_area_id, $photo_area_id;
	
	function get($tourID) {
		if (is_numeric($tourID) && $tourID > 0) {
			$q = "select * from Tours where ID = " . $tourID;
			$db = new db;
			$r = mysql_query($q);
			if (!$r) {
				return Error::MySQL();
			}
			$row = mysql_fetch_assoc($r);
			if ($row['ID']) {
				$to = new Tour;
				$to->ID = $row['ID'];		
				$to->start_date = $row['start_date'];
				$to->end_date = $row['end_date'];
				$to->title = $db->sanitize_from_db($row['title']);
				$to->description = $db->sanitize_from_db($row['description']);
				$to->is_active = $row['is_active'];
				$to->av_area_id = $row['av_area_id'];
				$to->photo_area_id = $row['photo_area_id'];
				return $to;
			}
		}
		
		return Error::create("Invalid tour ID.");
	}
	
	function getLatest() {
		$q = "select ID from Tours order by end_date desc limit 1";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		$row = mysql_fetch_assoc($r);
		return Tour::get($row['ID']);
	}
		
	function getID() {return $this->ID;}
	function getStartDate($dateMask = null) {
		$dateMask = (!$dateMask) ? DATE_FORMAT : $dateMask;
		return date($dateMask, strtotime($this->start_date));
	}
	function getEndDate($dateMask = null) {
		$dateMask = (!$dateMask) ? DATE_FORMAT : $dateMask;
		return date($dateMask, strtotime($this->end_date));
	}
	function getTitle() {return $this->title;}
	function getDescription() {return $this->description;}
	function isActive() {return $this->is_active;}
	function getAVAreaID() {return $this->av_area_id;}
	function getAVAreaObject() {
		if ($this->av_area_id > 0) {
			$ma = MediaArea::get($this->av_area_id);
			return $ma;
		} else {
			$e = new Error();
			$e->add("An administrator has not yet registered this tour for audio or videos.");
			return $e;
		}
	}
	function getPhotoAreaID() {return $this->photo_area_id;}
	function getPhotoAreaObject() {
		if ($this->photo_area_id > 0) {
			$ma = MediaArea::get($this->photo_area_id);
			return $ma;
		} else {
			$e = new Error();
			$e->add("An administrator has not yet registered this tour for photos.");
			return $e;
		}
	}	
	function activate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Tours set is_active = 1 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may activate tours.');
		}
	}
	
	function deactivate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Tours set is_active = 0 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may deactivate tours.');
		}
	}
	
	function remove() {
		if (User::isAdmin()) {
			$r = @mysql_query("delete from Tours where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may remove tours.');
		}
	}
	
	function update($postArray) {
		$db = new db;
		if (!User::isAdmin()) {
			return Error::create("Only an administrator may update tours.");
		}
		
		$title = $db->sanitize_to_db($postArray['title']);
		$start_date = $db->sanitize_to_db($postArray['start_date']);
		$end_date = $db->sanitize_to_db($postArray['end_date']);
		$sd = strtotime($start_date);
		$start_date = date('Y-m-d', $sd);
		$ed = strtotime($end_date);
		$end_date = date('Y-m-d', $ed);
		
		$description = $db->sanitize_to_db($postArray['description']);
		
		if (!$title) {
			$title = '(untitled tour)';
		}
		
		$r = @mysql_query("update Tours set title='$title', start_date='$start_date', end_date='$end_date', description = '$description' where ID = " . $this->ID);
		if ($r) {
			return Tour::get($this->ID);
		} else {
			return Error::MySQL();
		}
	}
	
	function add($postArray) {
		$db = new db;
		if (!User::isAdmin()) {
			return Error::create("Only an administrator may add tours.");
		}
		
		$title = $db->sanitize_to_db($postArray['title']);
		$start_date = $db->sanitize_to_db($postArray['start_date']);
		$end_date = $db->sanitize_to_db($postArray['end_date']);
		$sd = strtotime($start_date);
		$start_date = date('Y-m-d', $sd);
		$ed = strtotime($end_date);
		$end_date = date('Y-m-d', $ed);
		
		$description = $db->sanitize_to_db($postArray['description']);
		
		if (!$title) {
			$title = '(untitled tour)';
		}
				
		$r = @mysql_query("insert into Tours (title, start_date, end_date, description, is_active) values ('$title', '$start_date', '$end_date', '$description','" . DEFAULT_ACTIVE . "')");
		if ($r) {
			return Tour::get(mysql_insert_id());
		} else {
			return Error::MySQL();
		}
			

	}
	
	function getShows() {
		$q = "select Shows.ID from Shows, Tours where Shows.date >= Tours.start_date and Shows.date <= Tours.end_date and Tours.ID = {$this->ID} and Shows.is_active = 1 order by Shows.date asc";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		$showOArray = array();
		while($row = mysql_fetch_assoc($r)) {
			$showOArray[] = Show::get($row['ID']);
		}
		return $showOArray;
	}
	
	function registerMedia($mediaType) {
		$type = ($mediaType == 'av') ? 'av' : 'photos';
		if (User::isAdmin()) {
			if (($type == 'av' && $this->av_area_id < 1) || ($type == 'photos' && $this->photo_area_id < 1)) {
				$areaTitle = ($type == 'av') ? "Tour Audio/Video: " : "Tour Photos: ";
				$areaIDField = ($type == 'av') ? "av_area_id" : "photo_area_id";
				if ($type == 'av') {
					$allowed_extensions = MEDIA_EXTENSIONS_ALLOWED_AV;
					$display = "list";
				} else {
					$allowed_extensions = MEDIA_EXTENSIONS_ALLOWED_PHOTO;
					$display = "grid";
				}
				
				$ma = MediaArea::add(0, $areaTitle . $this->title, null, $allowed_extensions, $display);
				if (db::isError($ma)) {
					return $ma;
				}
				$ma->activate();
				$q = "update Tours set {$areaIDField} = " . $ma->getID() . " where ID = {$this->ID}";
				$r = mysql_query($q);
				if (!$r) {
					return Error::MySQL();
				} else {
					return $r;
				}
				return $r;
			} else {
				return Error::create("A tour with an ID of {$this->ID} is already registered.");
			}
		} else {
			return Error::create('Only an admin user may authorize a tour for media.');
		}
	}
	
	function validateMediaOperation($opType = "ADD", $mediaObject = null) {
		// called automatically by the m2 functions that add media, when passed the object
		// opType allows us to distinguish between adding, editing, updating, etc... but most times
		// the same check will work for all operation types
		if ($opType == "RESCAN") {
			// non-admins can't delete, only deactivate
			if (!User::isAdmin()) {
				return Error::create('Only an admin user may rescan media.');
			} else {
				return true;
			}
		} else {
			if (!User::isAdmin()) {
				return Error::create('Only an admin user may add, delete, activate or deactivate tour media.');
			} else {
				return true;
			}
		}
	}

}
?>